package com.barber.controller;

import com.barber.entity.Barber;
import com.barber.entity.Order;
import com.barber.mapper.BarberMapper;
import com.barber.service.BarberService;
import com.barber.service.OrderService;
import com.barber.util.XtUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerError;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    Logger logger=Logger.getLogger(OrderController.class);
    @Autowired
    OrderService orderService;

    @PostMapping("/insert")
    String insert(HttpServletRequest request, HttpServletResponse resp,@RequestBody Order order) throws ServerError, IOException {
        logger.debug("···········insert··········");
//
//        String data = request.getParameter("order");
//        logger.debug(data);
//        data = data.substring(1, data.length() - 1);
//        logger.debug(data);
//
//        Order o = JSON.parseObject(data, Order.class);
        if (orderService.insertOrder(order))
            return("OK");
        else
            return("FAIL");
    }
    @PostMapping()
    Map select(@RequestParam Map map, HttpServletRequest req) throws IOException {
        Map m  = (Map) req.getSession().getAttribute("islogin");
        logger.debug(m);//{password=lll, phone=111111, usertype=2, userid=3, username=哒玲玲}
        logger.debug(map);//{pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10}//
        int usertype= (int) m.get("usertype");
        map.put("usertype",usertype);
        if(usertype==2) {
            map.put("userid", m.get("userid"));//顾客id
        }else if(usertype==1){
            m.put("id",m.get("adminid")); map.put("id",m.get("id"));//id是理发师，和模糊查询时候用
        }else if(usertype==3){
            map.put("id",m.get("id"));
        }



         logger.debug(map);
        Map b=orderService.getOrders(map);
        logger.debug(b);
        return b;
    }
    @Autowired
    BarberMapper barberMapper;
    @Autowired
    BarberService barberService;
    @GetMapping("/getAllBarber")
    List<Barber> getAllBarber(HttpServletRequest request, HttpServletResponse resp, Barber barber) throws IOException {
        logger.debug("a");
        List<Barber> lst = barberService.selectAllBarber();
        return lst;
    }
    @GetMapping("/delete")//spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
    String delete(HttpServletResponse response, HttpServletRequest request,@RequestParam Map map ) throws IOException {
        logger.debug(map);
        if (orderService.deleteOrder(map)) {
            return ("success!");
        } else {
            return("delete failure!");

        }
    }
    @PostMapping("/update")
    String update(HttpServletRequest request, HttpServletResponse resp,@RequestBody Order order) throws IOException, ParseException {
        Map m  = (Map) request.getSession().getAttribute("islogin");
        logger.debug(m);
        int usertype= (int) m.get("usertype");
        logger.debug(usertype);

        if(usertype==1) {
            logger.debug(order);

            order.setBegintime(XtUtil.TimeConversion(order.getBegintime()));
            order.setEndtime(XtUtil.TimeConversion(order.getEndtime()));
            order.setUsertype(usertype);
        }else if(usertype==3){
            logger.debug(order);
            order.setBegintime(XtUtil.TimeConversion(order.getBegintime()));//begintime='2024-04-26T11:56:21.000Z', endtime='2024-04-26T13:56:23.000Z',

            order.setEndtime(XtUtil.TimeConversion(order.getEndtime()));
            order.setUsertype(usertype);
            logger.debug(order);
        }
        if(orderService.updateOrder(order))
            return("OK");
        else
            return("FAIL");
    }
    @PostMapping("/cancel")
    private String cancel(@RequestParam Map map) {
        logger.debug(map);//jixu
//        String id = request.getParameter("id");
//        logger.debug(id);
        if (orderService.cancelOrder(map))
//            return("OK");
            return "OK";
        else
//            return("FAIL");
            return "FAIL";
    }
    @PostMapping("/order")
    String order(HttpServletRequest request, HttpServletResponse resp,@RequestBody Order order)throws  IOException {
        logger.debug(order);
        Map m= (Map) request.getSession().getAttribute("islogin");
        logger.debug(m);
        order.setCustomerid((Integer) m.get("userid"));

        if (orderService.insertOrder(order))
            return("OK");
        else
            return("FAIL");

    }
    @PostMapping("/checktime")
    String checktime(HttpServletRequest request, HttpServletResponse resp,@RequestBody Order order)throws  IOException {
        logger.debug(order);
        Map m= (Map) request.getSession().getAttribute("islogin");
        order.setCustomerid((Integer) m.get("userid"));

        if (orderService.checktime(order))
            return("OK");
        else
            return("FAIL");

    }
}
